package com.zzc.util;

/**
 * @Description:
 * @author fengyi
 * @date 2018-08-22 18:09
 */
public class ApiResult {
    /**
     * ajax响应编码，200 表示正常请求，不存在非授权或者未登录请求
     */
    public static final int CODE_DEF = 200;

    /**
     * 未登录，前端判断后，跳转到登录页面
     */
    public static final int CODE_1100 = 1100;

    /**
     * 无权访问
     */
    public static final int CODE_1403 = 1403;

    private Integer code = CODE_DEF;

    private Boolean success = false;

    private String msg;

    private Object data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static ApiResult error() {
        ApiResult result = new ApiResult();
        result.setMsg("系统异常，请稍后重试");
        return result;
    }

    public static ApiResult error(String msg) {
        ApiResult result = new ApiResult();
        result.setMsg(msg == null ? "系统异常，请稍后重试" : msg);
        return result;
    }

    public static ApiResult error(String msg, Object data) {
        ApiResult result = new ApiResult();
        result.setMsg(msg == null ? "系统异常，请稍后重试" : msg);
        result.setData(data);
        return result;
    }

    public static ApiResult error(int code, String msg) {
        ApiResult result = new ApiResult();
        result.setCode(code);
        result.setMsg(msg == null ? "系统异常，请稍后重试" : msg);
        return result;
    }

    public static ApiResult error(int code, String msg, Object data) {
        ApiResult result = new ApiResult();
        result.setCode(code);
        result.setMsg(msg == null ? "系统异常，请稍后重试" : msg);
        result.setData(data);
        return result;
    }

    public static ApiResult ok() {
        ApiResult result = new ApiResult();
        result.setSuccess(true);
        return result;
    }

    public static ApiResult ok(String msg) {
        ApiResult result = new ApiResult();
        result.setSuccess(true);
        result.setMsg(msg);
        return result;
    }

    public static ApiResult ok(Object obj) {
        ApiResult result = new ApiResult();
        result.setSuccess(true);
        result.setData(obj);
        result.setMsg("ok");
        return result;
    }

    public static ApiResult ok(Object obj, String msg) {
        ApiResult result = new ApiResult();
        result.setSuccess(true);
        result.setData(obj);
        result.setMsg(msg);
        return result;
    }

    @Override
    public String toString() {
        return "ApiResult [code=" + code + ", success=" + success + ", msg=" + msg + ", data=" + data + "]";
    }

}
